<?php
class PortalWebService {
	// mc = memcache
	private $mc_servers;
	private $mc_timeout;
	private $server_config;

	private $timeless;
	private $time_now;
	private $time_shift;

	public function __construct($config, $timeless, $time_now, $time_shift) {
		$this->server_config = $config;
		$this->timeless = $timeless;
		$this->time_now = $time_now;
		$this->time_shift = $time_shift;
		$this->createMemcacheHandle();
	}


	// memcache functions
	private function createMemcacheHandle() {
		$this->mc_timeout = (int) $this->server_config->memcache->cache_time;

		$this->mc_handle = new Memcache();
		foreach ($this->server_config->memcache->farm as $v) {
			$v = (array) $v;
			$this->mc_handle->addServer($v['server'], $v['port']);
		}
	}

	private function createMemcacheKey($table_name, $keys) {
		return 'MyTv+PortalWS:'.$this->timeless.':'.$table_name.json_encode($keys);
	}

	private function getMemcache($key) {
		return @$this->mc_handle->get($key);
	}

	private function setMemcache($key, $value) {
		return $this->mc_handle->set($key, $value, 0, $this->mc_timeout);
	}

	private function deleteMemcache($key) {
		return $this->mc_handle->delete($key);
	}

	// real business
	public function __get($name) {
		if (in_array($name, array('content','statistic','membership','mytvplus','search','package'))) {
			$class_name = 'PortalWebServiceData'.ucfirst($name);
			if(is_null($this->$class_name)){
				$this->$class_name = new $class_name($this->server_config, $this->timeless, $this->time_now);
			}
			return $this->$class_name;
		}
	}

	public function __call($name, $arguments) {
		if (strpos($name, '_')) {
			$method_names = explode('_', $name);
			$last_return = @$arguments;
			foreach ($method_names as $method_name) {
				$last_return = call_user_func_array(array($this, $method_name), $last_return);
				if ($last_return < 0) {
					return $last_return;
				}
				$last_return = array($last_return);
			}
			return array_shift($last_return);
		}

		$name_info = explode('.',strtolower($name));
		if(count($name_info)>=2){
			$class = $this->$name_info[0];
			$function = '_'.$name_info[1];
		}else{
			$class = $this;
			$function = '_'.$name;
		}
		if(!method_exists($class,$function)){
			error_log("No such function: $name");
			return PortalWebServiceException::SYSTEM_ERROR; 
		}

		try{
			if ($this->time_shift || $this->timeless || in_array($name_info[0],array('membership','paymentstatus'))) {
				$value = call_user_func_array(array($class, $function), $arguments);
			} else {
				$key = $this->createMemcacheKey($name, $arguments);
				$value = $this->getMemcache($key);
				$value = false ;
				if (!$value) {
					$value = call_user_func_array(array($class, $function), $arguments);
					$this->setMemcache($key, $value);
				}
			}
		}catch(PortalWebServiceException $e){
			$code = $e->getCode();
			if(PortalWebServiceException::PARAMETERS_ERROR == $code){
				error_log("Parameter error:".$e->getMessage());
			}else{
				error_log("System error:".$e->getMessage());
				error_log("Trace as string:".$e->getTraceAsString());
			}
			return $e->getCode();
		}
		return $value;

		#case 'statistic.video':
		#case 'statistic.programme ':
	}

	public function _paymentstatus(){
	}

}
?> 
